package com.safeluck.floatwindow.audio;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;

/**
 * aaa
 * Created by lzw on 2019/8/29. 11:19:57
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class AudioRecordManager {

    private static final String TAG = "AudioRecordManager";
    private static AudioRecordManager instance;

    /***标记是否正在录音**/
    private boolean isRecording = false;

    private AudioRecord audioRecord;
    /***最小缓冲区大小*/
    private int bufferSize = 0;
    /***采样率*/
    private int sampleRateInHz = 44100;
    /***量化位数**/
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    /***存放音频数据的buffer**/
    private byte[] buffer;

    private int channelConf = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private OnAudioRecordListener onAudioRecordListener;


    private AudioRecordManager() {
        //计算最小缓冲区
        bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConf, audioFormat);
//        bufferSize = bufferSize > 320 ? 320 : bufferSize;

        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConf, audioFormat, bufferSize);
    }


    public static AudioRecordManager getInstance() {
        if (instance == null) {
            synchronized (AudioRecordManager.class) {
                if (instance == null) {
                    instance = new AudioRecordManager();
                }
            }
        }
        return instance;
    }

    /***
     * 开始采集音频
     */
    public void startRecording(final OnAudioRecordListener onAudioRecordListener) {
        setAudioRecordListener(onAudioRecordListener);
        if (audioRecord == null){
            audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConf, audioFormat, bufferSize);
        }
        buffer = new byte[bufferSize];
                new Thread(new Runnable() {
            @Override
            public void run() {
                isRecording = true;
                audioRecord.startRecording();
                while (isRecording){
                    Log.i(TAG, "run: buffer length"+buffer.length);
                    audioRecord.read(buffer,0,bufferSize);
                    if (onAudioRecordListener != null){
                        onAudioRecordListener.onVoiceRecord(buffer,bufferSize);
                    }
                }
                audioRecord.stop();
            }
        }).start();
    }

    /***
     * 停止音频采集
     */
    public void stopRecording(){
        Log.i(TAG, "stopRecording");
        isRecording = false;
        setAudioRecordListener(null);
    }

    public void destroy(){
        if (audioRecord!=null){
            audioRecord.release();
            audioRecord = null;
        }

    }

    public interface OnAudioRecordListener {
        /***
         * 采集到的音频信息回调到上层
         * @param data
         * @param size
         */
        void onVoiceRecord(byte[] data,int size);
    }

    private void setAudioRecordListener(OnAudioRecordListener onAudioRecordListener){
        this.onAudioRecordListener = onAudioRecordListener;
    }

}
